#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

void test_list()
{
    srand(time(0));
    const int N = 10000000;
    std::list<int> l;
    std::vector<int> v;
    for (int i = 0; i < N; i++)
    {
        auto e = rand() + i;
        l.push_back(e);
        v.push_back(e);
    }
    int begin1 = clock();
    std::sort(v.begin(), v.end());
    int end1 = clock();

    int begin2 = clock();
    l.sort();
    int end2 = clock();

    printf("vector sort: %d\n", end1 - begin1);
    printf("list sort: %d\n", end2 - begin2);
}
#include "list.h"
template<class T>
void print(list<T> lt)
{
    typename list<T>::iterator it = lt.begin();
    while (it != lt.end())
    {
        std::cout << *it << " ";
        ++it;
    }
    std::cout << '\n';
}
void test_list1()
{
    list<int> lt;
    lt.push_back(1);
    lt.push_back(2);
    lt.push_back(3);
    lt.push_back(4);
    print(lt);
    list<int> lt2 = lt;
    print(lt2);
}
void test_list2()
{
    list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    list<int>::iterator it = lt1.begin();
    while (it != lt1.end())
    {
        *it += 1;

        std::cout << *it << " ";
        ++it;
    }
    std::cout << std::endl;

    print(lt1);
}

struct Pos
{
    int _row;
    int _col;

    Pos(int row = 0, int col = 0)
        : _row(row)
        , _col(col)
    {}
};

void test_list3()
{
    list<Pos> lt1;
    lt1.push_back({1, 1});
    lt1.push_back({2, 2});
    lt1.push_back({3, 3});
    lt1.push_back({4, 4});

    //list<Pos>::iterator it = lt1.begin();
    auto it = lt1.begin();
    while (it != lt1.end())
    {
        //cout << (*it)._row << ":" << (*it)._col << endl;
        // 为了可读性，这里省略了一个->
        std::cout << it->_row << ":" << it->_col << std::endl;
        //cout << it.operator->()->_row << ":" << it.operator->()->_col << endl;
        ++it;
    }
    std::cout << std::endl;
}

void test_list4()
{
    list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    print(lt1);

    lt1.push_front(10);
    lt1.push_front(20);
    print(lt1);

    lt1.pop_back();
    lt1.pop_back();
    print(lt1);

    lt1.pop_front();
    lt1.pop_front();
    print(lt1);
}

void test_list5()
{
    list<int> lt1;
    lt1.push_back(1);
    lt1.push_back(2);
    lt1.push_back(3);
    lt1.push_back(4);
    print(lt1);

    list<int> lt2 = lt1;
    print(lt1);

    list<int> lt3 = {10, 20, 30, 40};
    lt1 = lt3;
    print(lt1);
}

void test_list6()
{
    list<int> lt1 = {10, 20, 30, 40};
    print(lt1);

    list<double> lt2 = {10.1, 20.1, 30.1, 40.1};
    print(lt2);
}

int main()
{
    test_list1();
    test_list2();
    test_list3();
    test_list4();
    test_list5();
    test_list6();
    return 0;
}